/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package uast.bpdex.actions.addBen;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import uast.bpdex.forms.addBen;
import uast.bpdex.BaseDatos.*;
import uast.bpdex.*;
import java.util.regex.*;
/**
 *
 * Clase utilizada para manipular el "submit" de un formulario de agregacion de benefactores
 */

public class addBenAction extends org.apache.struts.action.Action {
    
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        String target = "failAddU";
        String[] valores;
        int i;
        Pattern p;
        Matcher m;
        DataBase db;
        Benefactor ben;
        
        //Se verifica que, en efecto, se cuenta con un formulario para trabajar
        if (form == null)
            return mapping.findForward(target);
        
        //Se obtienen todos los valores del formulario
        
        addBen form0 = (addBen)form;
        valores = new String[9];
        valores[0] = form0.getPssw();
        valores[1] = form0.getUsr();
        valores[2] = form0.getNombre();
        valores[3] = form0.getUbicacion();
        valores[4] = form0.getRif();
        valores[5] = form0.getEmail();
        valores[6] = form0.getTlf();
        valores[7] = form0.getPregunta();
        valores[8] = form0.getRespuesta();
        
        //Se verifica que se hayan rellenado todos los campos
        
        for(i = 0; i < valores.length; ++ i)
            if(valores[i].equals(""))
                return mapping.findForward(target);
        
        //Se procede a validar los parametros
        
        //Se valida el RIF
        p = Pattern.compile("[j|J]-[0-9]+-[0-9]");
        m = p.matcher(valores[4]);
        if(!m.matches())
            return mapping.findForward(target);
        
        //Se valida el email
        p = Pattern.compile(".+@.+\\..+");
        m = p.matcher(valores[5]);
        if(!m.matches()){
          return mapping.findForward(target);
        }
        
        db = new DataBase();
        try{
            ben = new Benefactor(valores[0],valores[1],valores[2],valores[3],valores[4],valores[5],valores[6], valores[7], valores[8]);
            db.agregarBenefactor(ben);
            target = "succAddU";
        }catch(Exception ex0){
            return mapping.findForward(target); 
        }
        return mapping.findForward(target);
    }
}
